package sfsoftware.game.actor.enemy.bubblefish;

import sfsoftware.game.trevor.ui.Dialog;
import sfsoftware.util.Tile;

public class PingPongBubbleFish extends SimpleBubbleFish {

	public PingPongBubbleFish(int destX, int destY, String texture,	int destWidth, int destHeight, Tile[][] assocTileMap, int initDirection) {		
		super(destX, destY, texture, destWidth, destHeight, assocTileMap);
		
		if(initDirection == LEFT_DIRECTION || initDirection == RIGHT_DIRECTION)
		{
			this.setSrcX(40);
		}
		else
		{
			this.setSrcX(80);
		}	

		this.setSpeed(2);
		
		direction = initDirection;
	}
	
	public PingPongBubbleFish(int destX, int destY, String texture,	int destWidth, int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, destWidth, destHeight, assocTileMap);
		this.setSrcX(40);
		this.setSpeed(4);		
	}

	private int changeDirection()
	{
		int newDirection = 0;
		if(direction == DOWN_DIRECTION)
		{
			newDirection = UP_DIRECTION;
		}
		else if(direction == UP_DIRECTION)
		{
			newDirection = DOWN_DIRECTION;
		}
		else if(direction == LEFT_DIRECTION)
		{
			newDirection = RIGHT_DIRECTION;
		}
		else if(direction == RIGHT_DIRECTION)
		{
			newDirection = LEFT_DIRECTION;
		}
		return newDirection;
	}
	
	public boolean move(int dirX, int dirY, Tile[][] tileMap)
	{				
		if(!(sfsoftware.game.TrevorLegacy.isPaused) && !(Dialog.displayDialog))
		{
			int x = (int) (this.getX() + (width / 2));
			int y = (int) (this.getY() + (height / 2));
			
			int xTile = (int) Math.floor(((x - playingFieldX) / 40));
			int yTile = (int) Math.floor(((y - playingFieldY) / 40));			
			
			try {
				updateCornerVariables(x, y + (this.getSpeed() * dirY), tileMap);		 	
				if(!(collidingWithCast(dirX, dirY)))
				{
					if(dirY == -1)
					{
						if(topLeft && topRight)
						{
							super.move(this.getX(), this.getY() + (dirY * this.getSpeed()));
						}
						else
						{				
							super.move(this.getX(), playingFieldY + (yTile * 40));
							direction = changeDirection();
						}
					}
					if(dirY == 1)
					{
						if(bottomLeft && bottomRight)
						{
							super.move(this.getX(), this.getY() + (dirY * this.getSpeed()));
						}
						else
						{
							super.move(this.getX(), playingFieldY + (yTile * 40));
							direction = changeDirection();
						}			
					}			
	
					updateCornerVariables(x + (this.getSpeed() * dirX), y, tileMap);
					
					if(dirX == -1)
					{
						if(topLeft && bottomLeft)
						{
							super.move(this.getX() + (dirX * this.getSpeed()), this.getY());
						}
						else
						{
							super.move(playingFieldX + (xTile * 40), this.getY());	
							direction = changeDirection();
						}
					}
					if(dirX == 1)
					{
						if(topRight && bottomRight)
						{
							super.move(this.getX() + (dirX * this.getSpeed()), this.getY());
						}
						else
						{
							super.move(playingFieldX + (xTile * 40), this.getY());
							direction = changeDirection();
						}			
					}		
				}
				else
				{
					// Hit a non-walkable Actor!
					direction = changeDirection();
				}
			}
			catch(ArrayIndexOutOfBoundsException e)
			{
				direction = changeDirection();
			}
		}
		return true;
	}
}
